using System;
using UnityEngine;

namespace Framework.Timer
{
    /// <summary>
    /// 计时器Handler，延迟指定时间后触发callback
    /// </summary>
    public sealed class TimeHandler
    {

        private float         _maxTime;
        private float         _delayTime;
        private Action        _onComplete;
        private Action<float> _onUpdate;

        public bool IsDone => this._delayTime >= this._maxTime;

        internal TimeHandler(float delay, Action callback, Action<float> onUpdate = null)
        {
            this._maxTime    = delay;
            this._delayTime  = 0;
            this._onComplete = callback;
            this._onUpdate   = onUpdate;
        }

        internal void Update()
        {
            if (!this.IsDone)
            {
                this._onUpdate?.Invoke(this._delayTime / this._maxTime);
                this._delayTime += Time.deltaTime;
                if (this.IsDone)
                {
                    this._onComplete?.Invoke();
                }
            }
        }

        public void Kill() { this._delayTime = this._maxTime; }

    }
}